<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolBar" style="float:left;padding-top: 10px;padding-left: 50px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="sku">
          <el-input style="width: 8em" v-model="filters.sku" placeholder="SKU"></el-input>
        </el-form-item>
        <el-form-item prop="orderId">
          <el-input style="width: 8em" v-model="filters.orderId" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item prop="shop">
          <el-select v-model="filters.shop" clearable placeholder="店铺" style="width: 8em">
            <el-option v-for="item in shops"
                       :key="item"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="detailedDisposition">
          <el-select v-model="filters.detailedDisposition" clearable filterable placeholder="责任类型" style="width: 8em">
            <el-option v-for="item in dispositionType"
                       :key="item.label"
                       :label="item.value"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="reason">
          <el-select v-model="filters.reason" clearable filterable placeholder="退款原因" style="width: 8em">
            <el-option v-for="item in returnReasonType"
                       :key="item.label"
                       :label="item.value"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="filters.status" clearable filterable placeholder="处理结果" style="width: 8em">
            <el-option v-for="item in returnHandleType"
                       :key="item.label"
                       :label="item.value"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="returnMonth">
          <el-date-picker v-model="filters.returnMonth"
                          style="width: 8em"
                          type="month"
                          value-format="yyyy-MM"
                          placeholder="月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="sale:return:view"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="sale:return:view"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-filter"
                     label="列显示"
                     perms="sale:order:view"
                     @click="displayFilterColumnDialog"/>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns"
                                  @handleFilterColumns="handleFilterColumns">
      </table-column-filter-dialog>
    </div>

    <!--表格内容栏-->
    <kt-table :data="pageResult" :columns="filterColumns" :exec-summary="true" @findPage="findPage"></kt-table>
  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import TableColumnFilterDialog from '../core/TableColumnFilterDialog'

  export default {
    name: "Return",
    components: {
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: 'small',
        filters: {
          sku: '',
          orderId: '',
          shop: '',
          detailedDisposition: '',
          reason: '',
          status: '',
          returnMonth: ''
        },
        columns: [],//初始表格列显示字段
        filterColumns: [],//列显示过滤字段
        pageRequest: {},
        pageResult: {},
        shops: [], //店铺信息
        //退款订单责任划分类型 return_duty_define
        dispositionType: [],
        //退款原因类型 return_reason_type
        returnReasonType: [],
        //退款商品处理状态 return_handle_type
        returnHandleType: []
      }
    },
    methods: {
      // 处理表格列过滤显示
      initColumns() {
        this.columns = [
          {prop: "shop", label: "店铺", minWidth: 80},
          {prop: "orderId", label: "订单号", showOverflow: true, minWidth: 100},
          {prop: "quantity", label: "数量", minWidth: 70},
          {prop: "productName", label: "产品名称", showOverflow: true, minWidth: 100},
          {prop: "sku", label: "SKU", minWidth: 100},
        //  {prop: "fnSku", label: "FN-SKU", minWidth: 120},
          {prop: "asin", label: "ASIN", minWidth: 120},
          {prop: "disposition", label: "责任划分", showOverflow: true, minWidth: 100},
          {prop: "returnReason", label: "退款原因", showOverflow: true, minWidth: 100},
          {prop: "handleStatus", label: "处理结果", showOverflow: true, minWidth: 100},
          {prop: "customerComments", label: "买家留言", showOverflow: true, minWidth: 200},
          {prop: "returnDate", label: "退款日期", minWidth: 100},
          {prop: "createTime", label: "导入时间", minWidth: 135}
        ];
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      },
      findReturnTypes() {
        let returnDuty = 'return_duty_define';
        let returnReason = 'return_reason_type';
        let returnHandle = 'return_handle_type';
        let types = [returnDuty, returnReason, returnHandle];
        this.$api.dict.findByTypes(types).then((res) => {
          if (res.code === 200) {
            let typeMap = res.data;
            this.dispositionType = typeMap[returnDuty];
            this.returnReasonType = typeMap[returnReason];
            this.returnHandleType = typeMap[returnHandle];
          }
        });
      },
      //获取店铺信息
      findShops() {
        this.shops = localStorage.getItem('authorizedShops').split(',');
      },
      //处理表格列过滤显示
      displayFilterColumnDialog() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      //获取分页数据
      findPage(data) {
        if ((data !== null && typeof data !== 'undefined') && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          sku: this.filters.sku,
          shop: this.filters.shop,
          orderId: this.filters.orderId,
          reason: this.filters.reason,
          status: this.filters.status,
          returnMonth: this.filters.returnMonth,
          detailedDisposition: this.filters.detailedDisposition,
        };
        this.$api.returnOrder.findPage(this.pageRequest).then((res) => {
          if (res.code === 200) {
            this.pageResult = res.data;
            this.convertType(this.pageResult);
          }
        }).then((data !== null && typeof data !== 'undefined') ? data.callback : '');
      },
      convertType(pageResult) {
        let orderList = pageResult.content;
        for (let i = 0, len = orderList.length; i < len; i++) {
          let o = orderList[i];
          //责任划分转换
          this.dispositionType.forEach(d => {
            if (o.detailedDisposition === d.label) {
              o.disposition = d.value;
            }
            if (o.disposition === '') {
              o.disposition = o.detailedDisposition;
            }
          });
          //退款原因转换
          this.returnReasonType.forEach(r => {
            if (o.reason === r.label) {
              o.returnReason = r.value;
            }
            if (o.returnReason === '') {
              o.returnReason = o.reason;
            }
          });
          //处理状态转换
          this.returnHandleType.forEach(t => {
            if (o.status === t.label) {
              o.handleStatus = t.value;
            }
            if (o.handleStatus === '') {
              o.handleStatus = o.status;
            }
          });
        }
      },
      //重置搜索条件
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      }
    },
    mounted() {
      this.findShops();
      this.findReturnTypes();
      this.initColumns();
    }
  }
</script>

<style scoped>

</style>
